package work.supervision.platform.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import work.supervision.platform.dao.WorkTaskDao;
import work.supervision.platform.domain.SearchParam;
import work.supervision.platform.util.DateUtils;
import java.util.*;

@Controller
public class SysIndexContrller {

    @Autowired
    WorkTaskDao workTaskDao;

    @GetMapping("/")
    public String index(ModelMap modelMap){
        List<Map> list1 = workTaskDao.queryDeptNumByStatus("正在办理");
        modelMap.put("list1",list1);
        List<Map> list2 = workTaskDao.queryDeptNumByStatus("未办理");
        modelMap.put("list2",list2);
        List<Map> list3 = workTaskDao.queryDeptNumByStatus("长期工作");
        modelMap.put("list3",list3);
        List<Map> list4 = workTaskDao.queryDeptNumByStatus("已办结");
        modelMap.put("list4",list4);
        int listCount1=0,listCount2=0,listCount3=0,listCount4=0;
        if(list1.size() > 0){
            for (Map data:list1) {
                listCount1 += Integer.valueOf(data.get("num").toString());
            }
        }
        if(list2.size() > 0){
            for (Map data:list2) {
                listCount2 += Integer.valueOf(data.get("num").toString());
            }
        }
        if(list3.size() > 0){
            for (Map data:list3) {
                listCount3 += Integer.valueOf(data.get("num").toString());
            }
        }
        if(list4.size() > 0){
            for (Map data:list4) {
                listCount4 += Integer.valueOf(data.get("num").toString());
            }
        }
        modelMap.put("listCount1",listCount1);
        modelMap.put("listCount2",listCount2);
        modelMap.put("listCount3",listCount3);
        modelMap.put("listCount4",listCount4);
        return "index";
    }

    /**
     * 列表
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public Map list(SearchParam param){
        Map result = new HashMap();
        List<Map> rows = workTaskDao.list(param);
        for (Map data:rows) {
            data.put("swsjDate", DateUtils.formatDate((Date) data.get("swsj")));
            if(data.get("bjsj") == null){
                data.put("bjsjDate", null);
            }else{
                data.put("bjsjDate", DateUtils.formatDate((Date) data.get("bjsj")));
            }
            data.put("createDate", DateUtils.formatDate((Date) data.get("screate")));
            if(data.get("scbssj") == null){
                data.put("scbssjDate", null);
            }else{
                data.put("scbssjDate", DateUtils.formatDate((Date) data.get("scbssj")));
            }
        }
        result.put("code",0);
        result.put("msg","请求成功!");
        result.put("rows",rows);
        result.put("total",workTaskDao.count(param));
        //保存业务
        return result;
    }

    /**
     * 新建
     * @param modelMap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap){
        return "add";
    }


    /**
     * 业务新增保存
     * @param param
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Map save(SearchParam param){
        Map result = new HashMap();
        int rows = workTaskDao.add(param);
        if(rows>0){
            result.put("code",0);
            result.put("msg","保存成功!");
            result.put("data",null);
        }else{
            result.put("code",500);
            result.put("msg","保存失败!");
            result.put("data",null);
        }
        return result;
    }


    @GetMapping("/edit/{id}")
    public String edit(@PathVariable  Integer id, ModelMap modelMap){
        modelMap.put("vo",workTaskDao.selectById(id));
        return "edit";
    }

    /**
     * 编辑保存
     * @param param
     * @return
     */
    @PostMapping("/saveEdit")
    @ResponseBody
    public Map editSave(SearchParam param){
        Map result = new HashMap();
        int rows = workTaskDao.update(param);
        if(rows>0){
            result.put("code",0);
            result.put("msg","修改成功!");
            result.put("data",null);
        }else{
            result.put("code",500);
            result.put("msg","修改失败!");
            result.put("data",null);
        }
        return result;
    }

    @PostMapping("/remove")
    @ResponseBody
    public Map remove(String ids)
    {
        Map result = new HashMap();
        int rows = workTaskDao.delete(ids);
        if(rows>0){
            result.put("code",0);
            result.put("msg","删除成功!");
            result.put("data",null);
        }else{
            result.put("code",500);
            result.put("msg","删除失败!");
            result.put("data",null);
        }
        return result;
    }

}
